package com.xiaoxi.bms.controller.common;

import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.domain.common.FastDfs;
import com.xiaoxi.bms.service.common.fastdfs.FastDfsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author xiaoxi
 * @ProjectName springcloudbms
 * @PackageName com.xiaoxi.fastdfs.controller
 * @ClassName FastDfsController
 * @Description TODO
 * @Date 2023/2/9 18:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/upload/api/v1")
@Slf4j
public class FastDfsController {

    @Resource
    private FastDfsService fastDfsService;

    @PostMapping("image")
    public ApiResponse uploadImage(@RequestParam("file") final MultipartFile file) {
        try {
            FastDfs fastDfs = fastDfsService.uploadImage(file);
            return ApiResponse.ofSuccess(fastDfs);
        } catch (BMSException e) {
            log.error("出现异常：{}", e.getMessage());
            return ApiResponse.ofException(e);
        }
    }

    @PostMapping("excel")
    public ApiResponse uploadExcel(@RequestParam("file") final MultipartFile file) {
        try {
            FastDfs fastDfs = fastDfsService.uploadExcel(file);
            return ApiResponse.ofSuccess(fastDfs);
        } catch (BMSException e) {
            log.error("出现异常：{}", e.getMessage());
            return ApiResponse.ofException(e);
        }
    }

}